//
//  OO_EditAddressViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/29.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_EditAddressViewController.h"
#import "FSTextView.h"
#import "YJLocationPicker.h"
@interface OO_EditAddressViewController ()
@property (weak, nonatomic) IBOutlet UITextField *peopleTf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UIButton *cityButton;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet FSTextView *detailAdressTf;
@end

@implementation OO_EditAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfig];
}

-(void)UIConfig {
    self.title = @"修改收货地址";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:0 target:self action:@selector(deleteButtonPress)];
    rightItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.peopleTf.text = self.model.peopleName;
    self.phoneTf.text  = self.model.phone;
    [self.cityButton setTitle:self.model.city forState:0];
    self.detailAdressTf.text = self.model.adress;
    _saveBtn.backgroundColor = MainColor;
}

-(void)deleteButtonPress{
    
    WeakSelf
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除吗？" preferredStyle:UIAlertControllerStyleAlert];
   UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
       [weakSelf deleteAddress];
    }];
    UIAlertAction * cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:true completion:nil];
    



    
}
-(void)deleteAddress{
    
    WeakSelf
    JQFMDB *fmdb = [JQFMDB shareDatabase];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if ( [fmdb jq_deleteTable:ADDRESSDATABASE  whereFormat:@"where ID = '%@'",weakSelf.model.ID]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showTipMessageInWindow:@"删除成功"];
                if (weakSelf.deleteSuccess) {
                    weakSelf.deleteSuccess();
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
                
            });
        }
        else{
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showTipMessageInWindow:@"删除失败,请重试"];
                
                
            });
            
        }
    });
    
    
}
- (IBAction)chooseAddressButtonClicked:(UIButton *)sender {
    [self.view endEditing:YES];
    //直接调用
    [[[YJLocationPicker alloc] initWithSlectedLocation:^(NSArray *locationArray) {
        
        //array里面放的是省市区三级
        NSLog(@"%@", locationArray);
        //拼接后给button赋值
        [sender setTitle:[locationArray componentsJoinedByString:@""] forState:UIControlStateNormal];
        
    }] show];
    
}
- (IBAction)saveButtonPress:(UIButton *)sender {
    
    if (!self.peopleTf.text.length) {
        [MBProgressHUD showErrorMessage:@"请输入收货人姓名"];
        return;
    }
    
    if (!self.phoneTf.text.length) {
        [MBProgressHUD showErrorMessage:@"请输入手机号码"];
        return;
    }
    else{
        if (![CommonAPI telPhoneCheck:self.phoneTf.text]) {
            [MBProgressHUD showErrorMessage:@"手机号码不正确"];
            return;
        }
        
    }
    if ([self.cityButton.titleLabel.text isEqualToString:@"选择地区"]) {
        [MBProgressHUD showErrorMessage:@"请选择您所在的区域"];
        return;
    }
    
    if (![self.detailAdressTf.text length]) {
        [MBProgressHUD showErrorMessage:@"请输入您的详细地址"];
        return;
    }
    
    
    JQFMDB *fmdb = [JQFMDB shareDatabase];
    OO_AddressModel *model = [OO_AddressModel new];
    
    model.peopleName = self.peopleTf.text;
    model.phone = self.phoneTf.text;
    model.city = self.cityButton.titleLabel.text;
    model.adress = [NSString stringWithFormat:@"%@",self.detailAdressTf.text];
    model.ID = [CommonAPI getOnlyId];
    model.zhangHao = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
    WeakSelf
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if ( [fmdb jq_updateTable:ADDRESSDATABASE dicOrModel:model whereFormat:@"where ID = '%@'",self.model.ID]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showTipMessageInWindow:@"修改成功"];
                if (weakSelf.editSuccess) {
                    weakSelf.editSuccess();
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
                
            });
        }
        else{
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showTipMessageInWindow:@"修改失败,请重试"];
                
                
            });
            
        }
    });
    
    
    
    
}


@end
